﻿using DemoApp1.Attribute;
using DemoApp1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DemoApp1.Controllers
{
    [Authorize]
    public class HomeController : BaseController
    {

        public ActionResult Index()
        {
            var manage = new ManageModel();
           var listdata = GetIndexData();
            //if (filter != null)
            //{
            //    if (!string.IsNullOrWhiteSpace(filter.StudentName))
            //    {
            //        listdata = listdata.Where(p => p.StudentName.Contains(filter.StudentName)).ToList();
            //    }
            //    if (!string.IsNullOrWhiteSpace(filter.StduentNo))
            //    {
            //        listdata = listdata.Where(p => p.StudentNo.Contains(filter.StduentNo)).ToList();
            //    }
            //}
            manage.IndexModel = listdata;
            return View(manage);
        }

        [HttpPost]
        public ActionResult Search(SearchModel filter)
        {
            var manage = new ManageModel();
            var listdata = GetIndexData();
            if (filter != null)
            {
                if (!string.IsNullOrWhiteSpace(filter.StudentName))
                {
                    listdata = listdata.Where(p => p.StudentName.Contains(filter.StudentName)).ToList();
                }
                if (!string.IsNullOrWhiteSpace(filter.StduentNo))
                {
                    listdata = listdata.Where(p => p.StudentNo.Contains(filter.StduentNo)).ToList();
                }
            }
            manage.IndexModel = listdata;
            return View("Index", manage);
        }

        [HttpGet]
        public ActionResult Detail(int id)
        {
            var data = GetIndexData().Where(p => p.ID == id).FirstOrDefault();
            return View(data);
        }


        [HttpPost]
        public JsonResult GetStudent(int id)
        {
            var data = GetIndexData().Where(p => p.ID == id).FirstOrDefault();
            return Json(new { result = data }, JsonRequestBehavior.AllowGet);
        }
        public List<IndexModel> GetIndexData()
        {
            var list = new List<IndexModel>();
            list.Add(new IndexModel
            {
                ID = 1,
                ClassName = "初2016级1班",
                CreateDate = DateTime.Now,
                EduYear = "2016级第二学期",
                StudentName = "张莹",
                StudentNo = "106307102"
            });
            list.Add(new IndexModel
            {
                ID = 2,
                ClassName = "初2016级1班",
                CreateDate = DateTime.Now,
                EduYear = "2016级第二学期",
                StudentName = "李月",
                StudentNo = "106307103"
            });
            list.Add(new IndexModel
            {
                ID = 3,
                ClassName = "初2016级1班",
                CreateDate = DateTime.Now,
                EduYear = "2016级第二学期",
                StudentName = "陈秀红",
                StudentNo = "106307104"
            });
            list.Add(new IndexModel
            {
                ID = 4,
                ClassName = "初2016级1班",
                CreateDate = DateTime.Now,
                EduYear = "2016级第二学期",
                StudentName = "王艳红",
                StudentNo = "106307105"
            });
            list.Add(new IndexModel
            {
                ID = 5,
                ClassName = "初2016级1班",
                CreateDate = DateTime.Now,
                EduYear = "2016级第二学期",
                StudentName = "李勇",
                StudentNo = "106307106"
            });
            list.Add(new IndexModel
            {
                ID = 6,
                ClassName = "初2016级1班",
                CreateDate = DateTime.Now,
                EduYear = "2016级第二学期",
                StudentName = "孙淼",
                StudentNo = "106307107"
            });
            list.Add(new IndexModel
            {
                ID = 7,
                ClassName = "初2016级1班",
                CreateDate = DateTime.Now,
                EduYear = "2016级第二学期",
                StudentName = "朱磊",
                StudentNo = "106307108"
            });
            list.Add(new IndexModel
            {
                ID = 8,
                ClassName = "初2016级1班",
                CreateDate = DateTime.Now,
                EduYear = "2016级第二学期",
                StudentName = "邱超",
                StudentNo = "106307109"
            });
            list.Add(new IndexModel
            {
                ID = 9,
                ClassName = "初2016级1班",
                CreateDate = DateTime.Now,
                EduYear = "2016级第二学期",
                StudentName = "刘可",
                StudentNo = "106307110"
            });
            return list;
        }

    }
}